package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.maiji.cloud.entities.shopingmall.*;
import com.maiji.cloud.mapper.ShoppingOrderRefundMapper;
import com.maiji.cloud.microservice.UploadRecordService;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.shopingmall.OrderFindAllReqData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.UploadImageResData;
import com.maiji.cloud.response.shopingmall.ShopingOrderResData;
import com.maiji.cloud.response.shopingmall.ShoppingOrderRefundResData;
import com.maiji.cloud.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ShopingOrderRefundServiceImpl extends ServiceImpl<ShoppingOrderRefundMapper, ShoppingOrderRefundEntity> implements ShopingOrderRefundService {

    @Autowired
    private ShopingOrderService shopingOrderService;
    @Autowired
    private UploadRecordService uploadRecordService;

    @Override
    public ShoppingOrderRefundResData findResDataByOrderId (String orderId) {
        Wrapper<ShoppingOrderRefundEntity> wrapper = new EntityWrapper<ShoppingOrderRefundEntity>()
                .eq("order_id", orderId);
        ShoppingOrderRefundEntity shoppingOrderRefundEntity = selectOne(wrapper);
        ShoppingOrderRefundResData shoppingOrderRefundResData = new ShoppingOrderRefundResData();
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("belongIds", Arrays.asList(shoppingOrderRefundEntity.getUuId()));
        List<UploadImageResData> uploadImageResDataList = uploadRecordService.findResDataList(map);
        shoppingOrderRefundResData.setRefundImages(uploadImageResDataList);
        BeanUtils.copyProperties(shoppingOrderRefundEntity, shoppingOrderRefundResData);
        return shoppingOrderRefundResData;
    }

    @Override
    public BaseMetaResDto<List<ShopingOrderResData>> findAllRefundOrders(OrderFindAllReqData orderFindAllReqData, ReqMetaData metaData, String userId) {
        BaseMetaResDto<List<ShopingOrderResData>> baseMetaResDto = shopingOrderService.findAllOrders(orderFindAllReqData, metaData, userId);
        List<ShopingOrderResData> shopingOrderResDataList = baseMetaResDto.getData();
        if (shopingOrderResDataList.size() == 0) return new BaseMetaResDto<>(Status.SUCCESS);
        List<String> orderIds = shopingOrderResDataList.parallelStream().map(ShopingOrderResData::getUuId).collect(Collectors.toList());
        List<ShoppingOrderRefundEntity> soreList = selectList(new EntityWrapper<ShoppingOrderRefundEntity>().in("order_id", orderIds));
        // 处理一对多：子类维护关联关系
        List<String> soreIds = soreList.parallelStream().map(ShoppingOrderRefundEntity::getUuId).collect(Collectors.toList());
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("belongIds", soreIds);
        List<UploadImageResData> uploadImageResDataList = uploadRecordService.findResDataList(map);
        Map<String, List<UploadImageResData>> imgListMap = uploadImageResDataList.parallelStream().collect(Collectors.groupingBy(UploadImageResData::getBelongId));
        // 处理一对一：子类维护关联关系
        Map<String, ShoppingOrderRefundResData> sorMap = soreList.parallelStream().collect(Collectors.toMap(ShoppingOrderRefundEntity::getOrderId, sore -> {
            ShoppingOrderRefundResData shoppingOrderRefundResData = new ShoppingOrderRefundResData();
            BeanUtils.copyProperties(sore, shoppingOrderRefundResData);
            List<UploadImageResData> imageResDataList = imgListMap.get(sore.getUuId());
            return shoppingOrderRefundResData.setRefundImages(imageResDataList);
        }));
        shopingOrderResDataList.parallelStream().forEach(shopingOrderResData -> {
            ShoppingOrderRefundResData shoppingOrderRefundResData = sorMap.get(shopingOrderResData.getUuId());
            shopingOrderResData.setRefundData(shoppingOrderRefundResData);
        });
        return baseMetaResDto.setData(shopingOrderResDataList);
    }

}
